import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import BootAnimation from '../views/BootAnimation'
import ForgetPasswd from '../views/ForgetPasswd'
import BusinessCooperation from '../views/BusinessCooperation'
import Index from "@/layout/Index";
import Login from "@/views/Login";
// import store from '../store/index'
import {Notify} from "vant";
import {userInfo} from '../api/api'

Vue.use(VueRouter)

const routes = [

    //    index 路由
    {
        path: '/index',
        name: 'Index',
        component: Index,
        children: [

            //home路由
            {
                path: '/',
                name: 'Home',
                component: Home
            },
            //搜索路由
            {
                path: '/search',
                name: 'Search',
                component: () => import(/* webpackChunkName: "about" */ '../views/Search.vue')
            },
            //发帖路由
            {
                path: '/post',
                name: 'Post',
                component: () => import(/* webpackChunkName: "about" */ '../views/Post.vue')
            },
            //关于我路由
            {
                path: '/about',
                name: 'AboutMe',
                component: () => import(/* webpackChunkName: "about" */ '../views/AboutMe.vue')
            },
            //我的路由
            {
                path: '/mine',
                name: 'Mine',
                component: () => import(/* webpackChunkName: "about" */ '../views/Mine.vue')
            },
            //帖子详情页
            {
                path: '/nodeDetail',
                name: 'NodeDetail',
                component: () => import(/* webpackChunkName: "about" */ '../views/NodeDetail')
            },
            //作者详情
            {
                path: '/author_detail',
                name: 'AuthorDetail',
                component: () => import(/* webpackChunkName: "about" */ '../views/AuthorDetail')
            },
            //小小解梦
            {
                path: '/open_dream',
                name: 'OpenDream',
                component: () => import(/* webpackChunkName: "about" */ '../views/OpenDream')
            },
            //智能机器人
            {
                path: '/robot',
                name: 'Robot',
                component: () => import(/* webpackChunkName: "about" */ '../views/Robot')
            },
            //笑话
            {
                path: '/joke',
                name: 'joke',
                component: () => import(/* webpackChunkName: "about" */ '../views/Joke')
            },
            //星座配对
            {
                path: '/constellation',
                name: 'Constellation',
                component: () => import(/* webpackChunkName: "about" */ '../views/Constellation')
            },
        ]
    },
    //登录
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    //开场动画  试运行----------------------------------》
    //修改为默认启动界面
    {
        // path: '/boot_animation',
        path: '',
        name: 'BootAnimation',
        component: BootAnimation

    },
    //忘记密码
    {
        path: '/forget_passwd',
        name: 'ForgetPasswd',
        component: ForgetPasswd
    },
    //商务合作
    {
        path: '/bussiness_cooperation',
        name: 'BussinessCopperation',
        component: BusinessCooperation
    },

]

const router = new VueRouter({
    routes
})


export default router

// const routerPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//     return routerPush.call(this, location).catch(error=> error)
// }
//路由守卫
router.beforeEach((to, from, next) => {
    // console.log(to)
    // console.log(from)
    // console.log(next)


    userInfo().then(res => {
        // console.log(res)
        if (res.code == 0) {
            // console.log('登录')
            next()
        } else {
            if (to.path == '/mine') {
                next('/login');
                Notify({type: 'danger', message: '未登录！'});
            } else if (to.path == '/post') {
                next('/index')
                Notify({type: 'danger', message: '未登录！'});
            } else if (to.path == '/about') {
                next('/index')
                Notify({type: 'danger', message: '未登录！'});
            } else {
                next()
            }

        }
    })


})
